package com.qfedu.controller;

import com.qfedu.constants.Status;
import com.qfedu.dto.DeveloperDto;
import com.qfedu.model.Contractor;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.page.Result;
import com.qfedu.service.IContractorService;
import com.qfedu.vo.ConProVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/6/29 19:49
 */

@RestController
@RequestMapping("contractor")
public class ContractorController {

    @Autowired
    private IContractorService contractorService;

    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPage) {
        PageResult page = contractorService.findPage(queryPage);
        return page;
    }

    @PostMapping("add")
    public Result addContractor(String cname, int[] pids) {

        int row = contractorService.addAndBind(cname, pids);
        System.out.println(row);
        if (row > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    @GetMapping("findByCid")
    public Result findByCid(@RequestParam("cid") String cid) {
        Contractor contractor = contractorService.findByCid(cid);
        Result result = new Result();
        result.setMessage(Status.FAIL.getMessage());
        if (ObjectUtils.isNotEmpty(contractor)) {
            result.setFlag(true);
            result.setData(contractor);
            result.setMessage(Status.SUCCESS.getMessage());
        }
        return result;
    }

    @PostMapping("edit")
    public Result edit(@RequestBody ConProVO conProVO) {
        int row = contractorService.updateAndBind(conProVO);
        if (row > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    @GetMapping("delete")
    public Result delete(@RequestParam("cid") String cid) {
        int row = contractorService.deleteByCid(cid);
        if (row > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }
}
